package com.caseProject.puremvc.mediator.view
{
	import com.caseProject.puremvc.mediator.events.MenuEditOptionsEvent;
	import com.caseProject.puremvc.mediator.gui.Element;
	import com.caseProject.puremvc.mediator.gui.Transition;
	import com.caseProject.puremvc.model.Diagram;
	import com.caseProject.puremvc.model.vo.ElementVO;
	import com.caseProject.puremvc.model.vo.TransitionVO;
	
	import fl.controls.Button;
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.data.DataProvider;
	import fl.events.ComponentEvent;
	
	import flash.display.Sprite;
	
	public class MenuEditOptionsView extends Sprite
	{
		private var dataGrid:DataGrid;
		private var dataProv:DataProvider;
		private var button:Button;
		private var currentEditElem:Element;
		private var currentEditTrans:Transition;
		private var currentDiagramVO:Diagram;
		
		public function MenuEditOptionsView()
		{
			super();
			
			this.dataGrid = new DataGrid();
			this.dataProv = new DataProvider();
			this.button = new Button();
			
			this.dataGrid.dataProvider = this.dataProv;
			this.currentDiagramVO = new Diagram();
									
			this.button.y = this.dataGrid.height + 5;
			this.button.label = "Aplicar alterações";
		}
		
		override public function set width (num:Number):void
		{
			this.dataGrid.width = num;
			this.button.width = num;
		}
		
		public function removeMenuEditOption():void
		{
			if (this.contains(this.dataGrid))
			{
				this.removeChild(this.dataGrid);
				this.removeChild(this.button);
			}
		}
		
		public function changeBehaviorRenameDiagram(diagram:Diagram):void
		{
			this.currentDiagramVO = diagram;
			
			this.addChild(this.dataGrid);
			this.addChild(this.button);
			
			var colunaA:DataGridColumn = new DataGridColumn();
			var colunaB:DataGridColumn = new DataGridColumn();
			
			colunaA.dataField = "colunaA";
			colunaB.dataField = "colunaB";
			
			colunaA.sortable = false;
			colunaB.sortable = false;
			
			this.dataGrid.editable = true;
			colunaA.editable = false;
			colunaB.editable = true;
			
			this.dataGrid.columns = [colunaA, colunaB];
			this.dataProv = new DataProvider();
			this.dataGrid.dataProvider = this.dataProv;
			
			this.dataProv.addItem({colunaA:"Novo nome:"});			
			this.button.addEventListener(ComponentEvent.BUTTON_DOWN, onButtonDownRename);
		}
		
		protected function onButtonDownRename(event:ComponentEvent):void
		{
			this.currentDiagramVO.name = this.dataProv.getItemAt(0)["colunaB"];
			this.dispatchEvent(new MenuEditOptionsEvent(MenuEditOptionsEvent.RENAME_DIAGRAM_SUCESS));
			this.removeMenuEditOption();
			this.button.removeEventListener(ComponentEvent.BUTTON_DOWN, onButtonDownRename);
		}
		
		public function changeBehaviorEditState(elem:Element):void
		{
			this.currentEditElem = elem;
			
			this.addChild(this.dataGrid);
			this.addChild(this.button);
			
			var elemVO:ElementVO = this.currentEditElem.vo as ElementVO;
						
			var colunaA:DataGridColumn = new DataGridColumn("Parâmetro");
			var colunaB:DataGridColumn = new DataGridColumn("Valor");
			
			colunaA.dataField = "colunaA";
			colunaB.dataField = "colunaB";
			
			colunaA.sortable = false;
			colunaB.sortable = false;
			
			this.dataGrid.editable = true;
			colunaA.editable = false;
			colunaB.editable = true;
			
			this.dataGrid.columns = [colunaA, colunaB];
			this.dataProv = new DataProvider();
			this.dataGrid.dataProvider = this.dataProv;
			 
			this.dataProv.addItem({colunaA:"name", colunaB:elemVO.name});
			this.dataProv.addItem({colunaA:"entry", colunaB:elemVO.entry});
			this.dataProv.addItem({colunaA:"doWhile", colunaB:elemVO.doWhile});
			this.dataProv.addItem({colunaA:"exit", colunaB:elemVO.exit});
			
			this.button.addEventListener(ComponentEvent.BUTTON_DOWN, onButtonDownState);
		}
				
		protected function onButtonDownState(event:ComponentEvent):void
		{
			var elemVO:ElementVO = this.currentEditElem.vo as ElementVO;
			
			elemVO.name = this.dataProv.getItemAt(0)["colunaB"];
			elemVO.entry = this.dataProv.getItemAt(1)["colunaB"];
			elemVO.doWhile = this.dataProv.getItemAt(2)["colunaB"];
			elemVO.exit = this.dataProv.getItemAt(3)["colunaB"];
			
			this.currentEditElem.rename(elemVO);
			this.removeMenuEditOption();
			this.button.removeEventListener(ComponentEvent.BUTTON_DOWN, onButtonDownState);
		}
		
		public function changeBehaviorEditTransition(trans:Transition):void
		{
			this.currentEditTrans = trans;
			
			this.addChild(this.dataGrid);
			this.addChild(this.button);
			
			var transVO:TransitionVO = this.currentEditTrans.vo as TransitionVO;
			
			var colunaA:DataGridColumn = new DataGridColumn("Parâmetro");
			var colunaB:DataGridColumn = new DataGridColumn("Valor");
			
			colunaA.dataField = "colunaA";
			colunaB.dataField = "colunaB";
			
			this.dataGrid.editable = true;
			colunaA.editable = false;
			colunaB.editable = true;
			
			this.dataGrid.columns = [colunaA, colunaB];
			this.dataProv = new DataProvider();
			this.dataGrid.dataProvider = this.dataProv;
			
			this.dataProv.addItem({colunaA:"event", colunaB:transVO.event});
			this.dataProv.addItem({colunaA:"guard", colunaB:transVO.guard});
			this.dataProv.addItem({colunaA:"action", colunaB:transVO.action});
			
			this.button.addEventListener(ComponentEvent.BUTTON_DOWN, onButtonDownTransition);
		}
		
		protected function onButtonDownTransition(event:ComponentEvent):void
		{
			var transVO:TransitionVO = this.currentEditTrans.vo as TransitionVO;
			
			transVO.event = this.dataProv.getItemAt(0)["colunaB"];
			transVO.guard = this.dataProv.getItemAt(1)["colunaB"];
			transVO.action = this.dataProv.getItemAt(2)["colunaB"];
			
			this.removeMenuEditOption();
			this.button.removeEventListener(ComponentEvent.BUTTON_DOWN, onButtonDownTransition);
		}
		
		public function changeBehaviorNewDiagram():void
		{
			this.addChild(this.dataGrid);
			this.addChild(this.button);
			
			var coluna:DataGridColumn = new DataGridColumn("Tipo");
			coluna.dataField = "coluna";
			coluna.sortable = false;
			
			this.dataGrid.columns = [coluna];
			this.dataGrid.editable = false;
			
			this.dataProv = new DataProvider();
			this.dataGrid.dataProvider = this.dataProv;
			
			this.dataProv.addItem({coluna:"Diagrama de Estados"});
			this.dataProv.addItem({coluna:"Diagrama de Atividade"});
			
			this.button.addEventListener(ComponentEvent.BUTTON_DOWN, onButtonDownNew);
		}
		
		protected function onButtonDownNew(event:ComponentEvent):void
		{
			switch (this.dataGrid.selectedIndex)
			{
				case 0:
					this.dispatchEvent(new MenuEditOptionsEvent(MenuEditOptionsEvent.STATE));
					break;
				case 1:
					this.dispatchEvent(new MenuEditOptionsEvent(MenuEditOptionsEvent.ACTIVITY));
					break;
			}
			this.removeMenuEditOption();
			this.button.removeEventListener(ComponentEvent.BUTTON_DOWN, onButtonDownNew);
		}
	}
}